package fr.umlv.m2.jee.spij.service.ldap.authentification.impl;

import fr.umlv.m2.jee.spij.ldap.actors.Actor;
import fr.umlv.m2.jee.spij.ldap.actors.dao.ActorDAO;
import fr.umlv.m2.jee.spij.ldap.connection.LdapConnect;
import fr.umlv.m2.jee.spij.service.ldap.authentification.DisplayActor;
import fr.umlv.m2.jee.spij.service.ldap.authentification.IDefaultActorService;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import java.util.ArrayList;
import java.util.List;

/**
 * Provides methods to retrieve Actors into LDAP.
 *
 * @author lounes
 */
@Service("defaultActorService")
public class DefaultActorService implements IDefaultActorService {

    /**
     * Returns all the actor.
     *
     * @param dActor the actor to display.
     *
     * @return an actor.
     *
     * @throws NamingException if an error occured.
     */
    @Override
    @Transactional(readOnly = true)
    public final DisplayActor getCurrentUserDetails(final DisplayActor dActor)
            throws NamingException {
        try {
            DirContext ctx = LdapConnect.connect();
            ActorDAO actorDAO = new ActorDAO(ctx);
            Actor actor = actorDAO.getUserDetails(dActor.getLogin(),
                    dActor.getPassWord());
            if (actor == null) {
                return null;
            }
            DisplayActor displayActor = new DisplayActor(actor);
            return displayActor;
        } catch (Exception e) { // connection failed
            return null;
        }
    }

    /**
     * Returns all the student.
     *
     * @param context the message context.
     *
     * @return a list of actor.
     *
     * @throws NamingException if an error occured.
     */
    @Override
    @Transactional(readOnly = true)
    public final List<DisplayActor> getAllStudent(final MessageContext context)
            throws NamingException {
        DirContext ctx = LdapConnect.connect();
        ActorDAO actorDAO = new ActorDAO(ctx);
        List<Actor> actorList = actorDAO.getAllStudent();
        List<DisplayActor> displayActorList =
                new ArrayList<DisplayActor>(actorList.size());

        if (actorList.isEmpty()) {
            context.addMessage(
                    new MessageBuilder().info().code("actor.empty").build());
        } else {
            for (Actor actor : actorList) {
                displayActorList.add(new DisplayActor(actor));
            }
        }
        return displayActorList;
    }

    /**
     * Returns all the teacher.
     *
     * @param context the message context.
     *
     * @return a list of actor.
     *
     * @throws NamingException if an error occur during connection.
     */
    @Override
    @Transactional(readOnly = true)
    public final List<DisplayActor> getAllTeacher(final MessageContext context)
            throws NamingException {
        DirContext ctx = LdapConnect.connect();
        ActorDAO actorDAO = new ActorDAO(ctx);
        List<Actor> actorList = actorDAO.getAllTeacher();

        List<DisplayActor> displayActorList =
                new ArrayList<DisplayActor>(actorList.size());

        if (actorList.isEmpty()) {
            context.addMessage(
                    new MessageBuilder().info().code("actor.empty").build());
        } else {
            for (Actor actor : actorList) {
                displayActorList.add(new DisplayActor(actor));
            }
        }
        return displayActorList;
    }

}
